<template>
  <div class="home-box">
    <el-container>
      <el-header>
        <div class="left-box">
          <img src="../assets/heima.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <div>
          <el-row>
            <el-button type="info" @click="back">退出</el-button>
          </el-row>
        </div>
      </el-header>

      <el-container>
        <el-aside :width="!isCollapse ? 200 + 'px' : 70 + 'px'">
          <div class="top-box" @click="showMenus">|||</div>
          <el-menu
            background-color="#333744"
            active-text-color="#409eff"
            text-color="#fff"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath">
            <el-submenu :index="`${m.id}`" v-for="m in menus" :key="m.id">
              <!-- 一级菜单 -->
              <template slot="title">
                <i :class="icons[m.id]"></i>
                <span>{{m.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item 
                :index="'/' + c.path" v-for="c in m.children" 
                :key="c.id"
                @click="saveState('/' + c.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{c.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menus:[],
      icons:{
        125:'el-icon-user-solid',
        103:'el-icon-goods',
        101:'el-icon-s-cooperation',
        102:'el-icon-s-operation',
        145:'el-icon-s-platform'
      },
      isCollapse:false,
      activePath:''
    }
  },
  created(){
    this.getUserList()
    this.activePath = window.sessionStorage.getItem('path')
  },
  methods: {
    back() {
      this.$router.push('/login')
      window.sessionStorage.clear()
    },
    async getUserList(){
      const {data:res} = await this.$http.get('menus')
      //console.log(res)
      if(res.meta.status !== 200) return this.$message.error('获取菜单失败')
      this.menus = res.data
    },
    showMenus(){
      this.isCollapse = !this.isCollapse
    },
    saveState(path){
      window.sessionStorage.setItem('path',path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
.home-box {
  height: 100%;
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: rgb(55, 61, 65);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
  padding: 0;

  .left-box {
    display: flex;
    align-items: center;
    margin-left: 0;

    span {
      margin-left: 20px;
    }
  }
  .el-button {
    margin-right: 10px;
  }
}
.el-aside {
  background-color: rgb(51, 55, 68);
  color: #fff;

  .top-box{
    background-color: #4A5064;
    text-align: center;
    font-size: 20px;
    letter-spacing: 3px;
    height: 38px;
    line-height: 38px;
  }
}
.el-menu{
  border-right: none;
}
</style>